/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package geradores;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import dominio.Relatorio;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.text.html.HTML;
import org.w3c.dom.html.HTMLDocument;

/**
 *
 * @author gilcemar e naliane
 */
public class GeradorPdf {

    public String gerarPdf (List<Relatorio> relatorio) throws FileNotFoundException, DocumentException, IOException
    {
        String caminhoDoArquivo = "C:/Users/gilcemar e naliane/Documents/PASTA GILCEMAR/QUINTO PERÍODO/PADRÕES DE PROJETO/relatorio"+ relatorio.get(0).getMesAno()+".pdf";
        try
        {

            Document doc = new Document (PageSize.A4,85,56,85,56);
            HTMLDocument documento;
            FileOutputStream os = new FileOutputStream(caminhoDoArquivo);
            float total =0;
            PdfWriter.getInstance(doc, os);
            doc.open();
            Paragraph paragrafoTitulo = new Paragraph("Relatorio Mensal de Vendas");
            paragrafoTitulo.setAlignment(Element.ALIGN_CENTER);
            paragrafoTitulo.setSpacingAfter(20);
            PdfPTable table = new PdfPTable(new float []{0.3f,0.2f,0.2f,0.1f,0.1f,0.1f});
            PdfPCell header = new PdfPCell(new Paragraph("Nome"));
            PdfPCell header1 = new PdfPCell(new Paragraph("CPF"));
            PdfPCell header2= new PdfPCell(new Paragraph("Data de Compra"));
            PdfPCell header3 = new PdfPCell(new Paragraph("Modelo"));
            PdfPCell header4 = new PdfPCell(new Paragraph("Plano"));
            PdfPCell header5 = new PdfPCell(new Paragraph("Valor"));
            table.addCell(header);
            table.addCell(header1);
            table.addCell(header2);
            table.addCell(header3);
            table.addCell(header4);
            table.addCell(header5);
            doc.add(paragrafoTitulo);
            for (Relatorio r: relatorio)
            {
                total += r.getValor();
                DateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
                String dataFormatada = formato.format(r.getDatainicio());
                table.addCell(new Paragraph(r.getNome()));
                table.addCell(new Paragraph(r.getCpf()));
                table.addCell(new Paragraph(dataFormatada));
                table.addCell(new Paragraph(r.getModelo()));
                table.addCell(new Paragraph(r.getPlano().toString()));
                table.addCell(new Paragraph(r.getValor().toString()));
            }
            doc.add(table);
            PdfPTable tabela = new PdfPTable(2);
            tabela.addCell(new Paragraph("Total"));
            tabela.addCell(new Paragraph(String.valueOf(total)));
            doc.add(tabela);
            ConversorPdf pdfC = new ConversorPdf();
            pdfC.setEnderecoRecurso(caminhoDoArquivo);
            //documento = (HTMLDocument)doc;
            doc.close();
            os.close();
            return pdfC.getConteudo();
        }catch (NullPointerException nu)
        {
            throw new NullPointerException("Não houve vendas nesse mês");
        }finally  {

        }
        
    }
}
